<template>
  <hu-modal
    title="检修工作任务清单"
    :show-modal.sync="show"
    :loading="HuLoading"
    :setLook="setLook"
    @submit="addFrom"
  >
    <el-form
      ref="formData"
      :model="formData"
      :rules="rulesSon"
      :disabled="setLook === 0"
      label-width="140px"
    >
      <el-collapse v-model="MixinActiveNames" class="collapse_style">
        <el-collapse-item name="1">
          <template slot="title">
            <div>
              <span class="basic_div"></span
              ><span class="basic_title">基本信息</span>
            </div>
          </template>
          <div class="collapse_style_list">
            <el-row :gutter="50">
              <el-col :span="12">
                <el-form-item label="单位年份" prop="yearTime">
                  <el-date-picker
                    disabled
                    v-model="formData.yearTime"
                    type="year"
                    format="yyyy"
                    value-format="yyyy"
                    placeholder="选择年份"
                  >
                  </el-date-picker>
                </el-form-item>
              </el-col>
            </el-row>
          </div>
        </el-collapse-item>
        <el-collapse-item name="2">
          <template slot="title">
            <div>
              <span class="basic_div"></span
              ><span class="basic_title">信息</span>
            </div>
          </template>
          <div class="collapse_style_list" style="margin-bottom: 10px"></div>
        </el-collapse-item>
      </el-collapse>
    </el-form>
  </hu-modal>
</template>

<script>
export default {
  props: {
    showModal: {
      type: Boolean,
      default: false,
    },
    setLook: {
      default: "",
    },
    formFormData: {
      type: Object,
      default: () => ({}),
    },
  },
  watch: {
    showModal(val) {
      this.show = val;
      if (val) {
        this.getDefault();
        setTimeout(() => {
          this.getDetails();
        }, 100);
      }
    },
    show(val) {
      this.$emit("update:showModal", val);
    },
  },
  data() {
    return {
      formData: {},
      show: false,
      rulesSon: {
        yearTime: [
          { required: true, message: "单位年份不能为空", trigger: "change" },
        ],
      },
    };
  },
  created() {},
  methods: {
    // 初始化默认值
    getDefault() {
      this.formData = {
        yearTime: new Date().getFullYear() + "",
        workProcedures: "是",
      };
      this.resetForm("formData");
    },
    // 获取详情
    getDetails() {
      if (this.formFormData.worktaskId) {
        this.$startLoading();
        http
          .getDetails(this.formFormData.worktaskId)
          .then(({ result }) => {
            this.formData = result;
          })
          .finally(() => {
            this.$endLoading();
          });
      }
    },
    // 弹窗 提交
    addFrom() {
      this.$refs["formData"].validate((valid) => {
        if (valid) {
          this.$startLoading();
          let params = {
            ...this.formData,
          };
          if (this.formData.worktaskId != undefined) {
            http
              .updateList(params)
              .then(() => {
                this.$modal.msgSuccess("修改成功");
                this.show = false;
                this.$emit("getList");
              })
              .finally(() => {
                this.$endLoading();
              });
          } else {
            http
              .addList(params)
              .then(() => {
                this.$modal.msgSuccess("新增成功");
                this.show = false;
                this.$emit("HuSearchList");
              })
              .finally(() => {
                this.$endLoading();
              });
          }
        } else {
          this.$nextTick(() => {
            this.$utils.scrollIntoView();
          });
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped></style>
